<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>TranslateService</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/github.css" type="text/css" media="screen" />
<script src="../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        
        <h1>
            <span class="type">Class</span>
            TranslateService
            
                <span class="parent">&lt;
                    
                    Object
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../files/app/services/translate_service_rb.html">app/services/translate_service.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="#method-i-call">call</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="#method-c-new">new</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  
    <!-- Includes -->
    <div class="sectiontitle">Included Modules</div>
    <ul>
      
        <li>
          
            <a href="Serviceable.html">
              Serviceable
            </a>
          
        </li>
      
        <li>
          
            <a href="StatusObject.html">
              StatusObject
            </a>
          
        </li>
      
    </ul>
  



  

    

    

    


    


    <!-- Methods -->
    
      <div class="sectiontitle">Class Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-c-new">
            
              <b>new</b>(text)
            
            <a href="../classes/TranslateService.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>

          
            <div class="description">
              
            </div>
          

          

          

          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
                
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File app/services/translate_service.rb, line 4</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>(<span class="ruby-identifier">text</span>)
  <span class="ruby-ivar">@text</span> = <span class="ruby-identifier">text</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                  
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-call">
            
              <b>call</b>()
            
            <a href="../classes/TranslateService.html#method-i-call" name="method-i-call" class="permalink">Link</a>
          </div>

          
            <div class="description">
              
            </div>
          

          

          

          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-call_source')" id="l_method-i-call_source">show</a>
                
              </p>
              <div id="method-i-call_source" class="dyn-source">
                <pre><span class="ruby-comment"># File app/services/translate_service.rb, line 8</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">call</span>
  <span class="ruby-identifier">uri</span> = <span class="ruby-constant">URI</span>(<span class="ruby-identifier">$translate</span>[<span class="ruby-value">:host</span>])
  <span class="ruby-identifier">params</span> = {
              <span class="ruby-identifier">keyfrom</span><span class="ruby-operator">:</span> <span class="ruby-identifier">$translate</span>[<span class="ruby-value">:keyfrom</span>],
              <span class="ruby-identifier">key</span><span class="ruby-operator">:</span> <span class="ruby-identifier">$translate</span>[<span class="ruby-value">:key</span>],
              <span class="ruby-identifier">type</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;data&quot;</span>,
              <span class="ruby-identifier">doctype</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;json&quot;</span>,
              <span class="ruby-identifier">version</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;1.1&quot;</span>,
              <span class="ruby-identifier">q</span><span class="ruby-operator">:</span> <span class="ruby-ivar">@text</span><span class="ruby-comment">#.force_encoding(&quot;UTF-8&quot;)</span>
            }
  <span class="ruby-identifier">uri</span>.<span class="ruby-identifier">query</span> = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">encode_www_form</span>(<span class="ruby-identifier">params</span>)
  <span class="ruby-identifier">res</span> = <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">get_response</span>(<span class="ruby-identifier">uri</span>)
  <span class="ruby-identifier">result</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">res</span>.<span class="ruby-identifier">body</span>)
  <span class="ruby-keyword">case</span> <span class="ruby-identifier">result</span>[<span class="ruby-string">&quot;errorCode&quot;</span>]
  <span class="ruby-keyword">when</span> <span class="ruby-number">0</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-identifier">result</span>[<span class="ruby-string">&quot;translation&quot;</span>].<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-operator">-</span><span class="ruby-operator">&gt;</span>(<span class="ruby-identifier">i</span>){<span class="ruby-identifier">i</span>}).<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;\n&quot;</span>)
  <span class="ruby-keyword">when</span> <span class="ruby-number">20</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-string">&#39;要翻译的文本过长&#39;</span>
  <span class="ruby-keyword">when</span> <span class="ruby-number">30</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-string">&#39;无法进行有效的翻译&#39;</span>
  <span class="ruby-keyword">when</span> <span class="ruby-number">40</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-string">&#39;不支持的语言类型&#39;</span>
  <span class="ruby-keyword">when</span> <span class="ruby-number">60</span>
    <span class="ruby-identifier">message</span> = <span class="ruby-string">&#39;无词典结果，仅在获取词典结果生效&#39;</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-constant">Status</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">success</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-identifier">message</span>)
<span class="ruby-keyword">rescue</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
  <span class="ruby-constant">Status</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">success</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">message</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>
